using System.Collections;
using System.Collections.Generic;
using DG.Tweening;
using UnityEngine;
using UnityEngine.UI;

namespace View.Instant
{
    public class ToolTipsView : View
    {
        private class ToolTipsInfo
        {
            public CanvasGroup _canvasgroup;
            public GameObject _obj;
            public Text _txt;
            public long _time;
        }
        private GameObject mItem;
        private List<ToolTipsInfo> mTipsList = new List<ToolTipsInfo>();
        public override void OnLoad()
        {
            base.OnLoad();
            
            mItem = transform.Find("item").gameObject;
        }

        private void OnCreateObj(object[] args)
        {
            string tipscontent = (string)(args[0]);
            long tipstime = GameLocalInfo.OnGetNowTime();
            ToolTipsInfo tipsinfp = null;
            for (int i = 0; i < mTipsList.Count; i++)
            {
                if ((tipstime - mTipsList[i]._time) >= 1)
                {
                    mTipsList[i]._time = GameLocalInfo.OnGetNowTime();
                    tipsinfp = mTipsList[i];
                }
            }

            if (tipsinfp == null)
            {
                GameObject go = GameObject.Instantiate(mItem);
                go.transform.SetParent(mItem.transform.parent);
                go.transform.localPosition = Vector3.zero;
                go.transform.localScale = Vector3.one;
                go.transform.localRotation = Quaternion.identity;

                tipsinfp = new ToolTipsInfo();
                tipsinfp._obj = go;
                tipsinfp._canvasgroup = go.GetComponent<CanvasGroup>();
                tipsinfp._txt = go.transform.Find("tips").GetComponent<Text>();
                tipsinfp._time = GameLocalInfo.OnGetNowTime();
                mTipsList.Add(tipsinfp);
            }

            tipsinfp._canvasgroup.alpha = 1;
            tipsinfp._obj.transform.localPosition = new Vector3(0, 500, 0);
            
            tipsinfp._obj.gameObject.SetActive(true);
            tipsinfp._txt.text = tipscontent;
            tipsinfp._canvasgroup.DOFade(0, 0.2f).SetDelay(0.2f+0.5f);
            tipsinfp._obj.transform.DOLocalMove(new Vector3(0, 700, 0), 0.5f).SetDelay(0.5f).SetEase(Ease.Linear).OnComplete(() =>
            {
                tipsinfp._obj.gameObject.SetActive(false);
            });
        }

        public override void OnShow(params object[] args)
        {
            base.OnShow();
            OnCreateObj(args);
        }

        public override void OnClose(params object[] args)
        {
            base.OnClose();
            if (gameObject.activeSelf)
            {
            }
        }
    }
}

